# Courtesy of qemu-3dfx <liewkj@yahoo.com>

_realname=virglrenderer
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=0.10.4
pkgrel=1
pkgdesc='A virtual 3D GPU library, that allows the guest operating system to use the host GPU to accelerate 3D rendering'
arch=('any')
mingw_arch=('mingw64' 'mingw32' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url='https://virgil3d.github.io/'
license=(MIT)
depends=("${MINGW_PACKAGE_PREFIX}-libepoxy")
makedepends=("${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc")
_tag=$pkgver
source=("https://gitlab.freedesktop.org/virgl/${_realname}/-/archive/${_tag}/${_realname}-${_tag}.tar.bz2"
        "https://raw.githubusercontent.com/kjliew/qemu-3dfx/f4db048c5c2f19098774bab449ad40c0a3364847/virgil3d/MINGW-packages/0001-Virglrenderer-on-Windows-and-macOS.patch"
        "vrend_debug_cb.patch")
sha256sums=('b42dbbc87f799feb77f89a246336253aedc733f7c3c78d4103158438deea5a6d'
            'a2c39111c17f4bb1a59a7b2f9bd68fb19c8e25667ad7a75787a8b9719a760a07'
            'eac6d43835fcb576e724ca3df267702a11dd883cf0ce5917e36735da70b465ea')

noextract=("${_realname}-${_tag}.tar.bz2")

prepare() {
  [[ -d "${srcdir}/${_realname}-${_tag}" ]] && rm -rf "${srcdir}/$_realname-${_tag}"
  tar -C "${srcdir}" -xf "${srcdir}/${_realname}-${_tag}.tar.bz2" || true

  cd "${srcdir}/${_realname}-${_tag}"
  # https://gitlab.freedesktop.org/virgl/virglrenderer/-/issues/217
  patch -p2 -i "${srcdir}/0001-Virglrenderer-on-Windows-and-macOS.patch"
  if [[ "$MSYSTEM" == "MINGW32" ]] || [[ "$MSYSTEM" == "CLANG32" ]]
  then
    patch -p0 -i "${srcdir}/vrend_debug_cb.patch"
  fi
}

build() {
  cd "${_realname}-${_tag}"
  MSYS2_ARG_CONV_EXCL="--prefix" \
  meson \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    build

  ninja -C build
}

check() {
  cd "${_realname}-${_tag}"
  ninja -C build test || true
}

package() {
  cd "${_realname}-${_tag}"
  DESTDIR="$pkgdir" ninja -C build install
  install -D -m644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}/${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
